// pages/user-hyzx-ckxx/user-hyzx-ckxx.js
var util = require('../../utils/network.js')
Page({

  /**
   * 
   * 页面的初始数据
   */
  data: {
    pis:[],
    pagecount:1,
    page:1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
      var token = util.token
      var that=this
      wx.request({
          url: util.main + '/passenger/getPiList/0',
          data: {
              "page":1,
              "size":10,
              "token": token,
              "userId":wx.getStorageSync("userId")
          },
          header: {
              "content-type": "application/json",
              "accept": "application/json,",
          },
          method: "GET",
          dataType: "json",
          success: function (res) {
              that.setData({
                  pis: res.data.pis,
                  pagecount: res.data.pagecount
              })
              console.log(res)
          }
      })
  },
    delInfo:function(e){
        var that=this
        var index = e.target.dataset.index
        var pis=that.data.pis[index]
        var piIds = pis.id
        var token = util.token
        wx.request({
            url: util.main + '/passenger/deletePi/0',
            data: {
                "piIds": piIds,
                "userId": wx.getStorageSync('userId'),
                "deviceid": wx.getStorageSync('openId'),
                "token": token
            },
            header: {
                "content-type": "application/x-www-form-urlencoded",
                "accept": "application/json,",
            },
            method: "POST",
            dataType: "json",
            success: function (res) {
                if (res.statusCode == 200) {
                    wx.redirectTo({
                        url: '../user-hyzx-ckxx/user-hyzx-ckxx',
                    })
                }
                console.log(res)
            }
        })
    },
    changeInfo:function(e){
        var that=this
        var index=e.target.dataset.num
        var pis=that.data.pis
        var id=pis[index].id
        var name=pis[index].name
        var cardtype = pis[index].cardtype
        var idnum = pis[index].idnum
        var phonenum = pis[index].phonenum
        wx.navigateTo({
            url: '../user-hyzx-ckxx-xiugai/user-hyzx-ckxx-xiugai?id=' + id + '&name=' + name + '&cardtype=' + cardtype + '&idnum=' + idnum + '&phonenum=' + phonenum,
        })
    },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
  
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
      var token = util.token
      var that = this
      wx.request({
          url: util.main + '/passenger/getPiList/0',
          data: {
              "page": 1,
              "size": 10,
              "token": token,
              "userId": wx.getStorageSync("userId")
          },
          header: {
              "content-type": "application/json",
              "accept": "application/json,",
          },
          method: "GET",
          dataType: "json",
          success: function (res) {
              that.setData({
                  pis: res.data.pis
              })
              wx.stopPullDownRefresh()
          }
      })
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
      var token = util.token
      var that = this
      if (that.data.page < that.data. pagecount){
        that.setData({
            page: that.data.page+1
        })
        wx.request({
            url: util.main + '/passenger/getPiList/0',
            data: {
                "page": that.data.page,
                "size": 10,
                "token": token,
                "userId": wx.getStorageSync("userId")
            },
            header: {
                "content-type": "application/json",
                "accept": "application/json,",
            },
            method: "GET",
            dataType: "json",
            success: function (res) {  
                var list=res.data.pis
                that.setData({
                    pis: that.data.pis.concat(list)
                })
            }
        })
      } else if (that.data.page >= that.data. pagecount){
          wx.stopPullDownRefresh()
      }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})